package com.normaltest.che300escjl.utils;

import com.normaltest.che300escjl.dto.Car;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;

public class Elasticserch {

    static String httpHost = "192.168.80.129";
    static int httpPort= 9200;

    public Elasticserch() {
    }

    public static void setHttpHost(String httpHost) {
        Elasticserch.httpHost = httpHost;
    }

    public static void setHttpPort(int httpPort) {
        Elasticserch.httpPort = httpPort;
    }

    public static Car getToSearch(Car aCar) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(httpHost, httpPort, "http")));
            // 创建搜索请求

        Car bcar;
        try {
            SearchRequest searchRequest = new SearchRequest("che300");

            // 构建搜索源
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

            // 添加 term 查询条件  //TODO 修改位置
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("ziduan1", aCar.getZiduan1());
            boolQuery.must(termQueryBuilder);

            // 添加 match 查询条件  //TODO 修改位置
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("ziduan2", aCar.getZiduan2());
            boolQuery.must(matchQueryBuilder);

            // 设置查询条件
            sourceBuilder.query(boolQuery);

            // 设置搜索请求的源
            searchRequest.source(sourceBuilder);

            // 执行搜索请求
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            bcar = new Car();

            SearchHits hits = searchResponse.getHits();
            if(hits != null) {
                for (SearchHit hit : hits) {
                    // 获取 ziduan1 和 ziduan2 的值
                    String ziduan1 = hit.getSourceAsMap().get("ziduan1").toString();
                    String ziduan2 = hit.getSourceAsMap().get("ziduan2").toString();

                    // 输出结果
                    bcar.setZiduan1(ziduan1);
                    bcar.setZiduan2(ziduan2);
                    break;
                }
            }

            // 输出搜索结果
            System.out.println("Search response: " + searchResponse.toString());
        } finally {
            client.close();
        }
        return bcar;
    }
    public static void addToIndex(Car car) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.80.128", 9200, "http")));
        try {
            // 创建索引请求
            IndexRequest indexRequest = new IndexRequest("che300")
                    .source(XContentType.JSON, "ziduan1", car.getZiduan1(), "ziduan2", car.getZiduan2());

            // 执行索引请求
            IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);

            // 输出索引响应
            System.out.println("Index response: " + indexResponse.getResult());
        } finally {
            client.close();

        }
    }
    //批量插入
    public static void addCarsToIndex(List<Car> cars) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.80.128", 9200, "http")));
        try {
            // 创建 BulkRequest 对象
            BulkRequest bulkRequest = new BulkRequest();

            // 添加多个索引请求到 BulkRequest
            for (Car car : cars) {
                IndexRequest indexRequest = new IndexRequest("che300")
                        .source(XContentType.JSON, "ziduan1", car.getZiduan1(), "ziduan2", car.getZiduan2());
                bulkRequest.add(indexRequest);
            }

            // 执行 BulkRequest
            BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);

            // 检查是否有失败的请求
            if (bulkResponse.hasFailures()) {
                System.out.println("Bulk insert failed: " + bulkResponse.buildFailureMessage());
            } else {
                System.out.println("Bulk insert successful");
            }
        } finally {
            client.close();
        }
    }
}
